package exp3;

import java.util.LinkedList;

class Node<T> {
    public Node(T value) {
        this.value = value;
    }

    public Node() {
    }

    Node<T> prev, next;
    T value;
}

public class MyLinkedStack<T> implements Cloneable {

    private Node<T> head, tail;
    private LinkedList<T> list;
    private int size;

    public MyLinkedStack() {
        head = new Node<T>();
        tail = new Node<T>();
        list = new LinkedList<>();
        size = 0;
    }

    public void push(T t) {
        Node<T> newnode = new Node<>(t);
        head.next = newnode;
        list.addFirst(t);
        size++;
    }

    public void pop() {
        if (isEmpty()) {
            System.out.println("error");
        } else {
            list.removeFirst();
            size--;
        }
    }

    public T top() {
        return list.getFirst();
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public String toString() {
        return list.toString() + " size:" + size();
    }

    public static void main(String[] args) {
        MyLinkedStack<Integer> stack = new MyLinkedStack<>();
        for (int i = 0; i < 5; i++) {
            stack.push(i);
            System.out.println(stack);
        }
        stack.pop();
        System.out.println(stack);
    }

    public void clear() {
        head = new Node<T>();
        tail = new Node<T>();
        list = new LinkedList<>();
        size = 0;
    }
}